package com.xszx.controller;

import com.xszx.dao.BalanceDao;
import com.xszx.dao.HistoryDao;
import com.xszx.dao.InfoDao;
import com.xszx.dao.StartAccountDao;
import com.xszx.entity.Balance;
import com.xszx.entity.History;
import com.xszx.entity.Info;
import com.xszx.entity.StartAccount;
import com.xszx.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;
import javax.servlet.http.PushBuilder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class BankController {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private InfoDao infoDao;

    @Autowired
    private StartAccountDao startAccountDao;

    @Autowired
    private BalanceDao balanceDao;

    @Autowired
    private HistoryDao historyDao;

    @RequestMapping("/add")
    @ResponseBody
    @Transactional
    public Map addInfo(@RequestBody Info info){
        System.out.println("info = " + info);
        Map map = new HashMap();
        infoDao.save(info);
        map.put("msg","录入成功");
        return map;
    }

    @RequestMapping("/findInfo")
    @ResponseBody
    public Map findInfo(@RequestBody Info i){
        Map map = new HashMap();
        String idCard = i.getIdCard();
        //System.out.println("idCard = " + idCard);

        List<Info> list = infoDao.findAll();
        //System.out.println("list = " + list);
        Info info = infoDao.findOneByIdCard(idCard);
        //System.out.println("info = " + info);
        if (info!=null){
            map.put("flag",true);
            return map;
        }else {
            map.put("flag",false);
            map.put("msg","该用户信息不存在");
            return map;
        }
    }
    @RequestMapping("/addAccount")
    @ResponseBody
    public Map startAccount(@RequestBody StartAccount startAccount){
        Map map = new HashMap();
        //System.out.println("startAccount = " + startAccount);
        StartAccount sa = startAccountDao.findByAccount(startAccount.getAccount());
        if (sa != null) {
            map.put("flag",false);
            map.put("msg","该账户已存在");
            return map;
        }else {
            startAccount.setId(idWorker.nextId());
            startAccountDao.save(startAccount);
            map.put("flag",true);
            map.put("msg","开户成功");
            return map;
        }
    }

    @RequestMapping("/findAccount")
    @ResponseBody
    public Map findAccount(@RequestBody StartAccount sa, HttpSession session){
        Map map = new HashMap();
        String account = sa.getAccount();
        String accountType = sa.getAccountType();

        StartAccount startAccount = startAccountDao.findByAccountAndAccountType(account, accountType);
        if (startAccount!=null){
            map.put("flag",true);
            session.setAttribute("account",account);
            session.setAttribute("accountType",accountType);
            if (Integer.parseInt(accountType) == 1){
                map.put("info","/timeDeposit");
            }else if (Integer.parseInt(accountType) == 2){
                map.put("info","/demandDeposit");
            }
            return map;
        }else {
            map.put("flag",false);
            map.put("msg","该账号信息不存在");
            return map;
        }
    }

    @RequestMapping("/submitBalance")
    @ResponseBody
    public Map submitBalance(@RequestBody Balance balance, HttpSession session){
        Map map = new HashMap();
        int type = balance.getType();
        Double money = balance.getBalance();
        balance.setAccount(session.getAttribute("account").toString());

        System.out.println("balance = " + balance);

        //表中的数据
        Balance b = balanceDao.findByAccount(balance.getAccount());
        System.out.println("b = " + b);
        Double nowBalance = 0.0;
        Boolean flag = true;
        String msg = "操作成功";
        if (type == 1){
            if (b == null){
                balance.setId(idWorker.nextId());
                balanceDao.save(balance);
                historyDao.save(this.getHistory(balance, "存钱"));
            }else {
                //存款操作
                nowBalance = b.getBalance()+balance.getBalance();
                b.setBalance(nowBalance);
                balanceDao.save(b);
                historyDao.save(this.getHistory(balance, "存钱"));
            }
        }else {
            if (b == null){
                flag = false;
                msg = "账户余额不足";
                balance.setId(idWorker.nextId());
                balance.setBalance(0.0);
                balanceDao.save(balance);
            }else {
                //取款操作
                if (b.getBalance() < balance.getBalance()){
                    flag = false;
                    msg = "账户余额不足";
                }else {
                    nowBalance = b.getBalance()-balance.getBalance();
                    b.setBalance(nowBalance);
                    balanceDao.save(b);
                    historyDao.save(this.getHistory(balance, "取钱"));
                }
            }
        }
        map.put("flag",flag);
        map.put("msg",msg);
        return map;

    }

    @RequestMapping("/find")
    @ResponseBody
    public Map find(){

        Map map = new HashMap();
        Page<History> page = historyDao.findAll(PageRequest.of(0, 5));
        List<History> list = page.getContent();
        map.put("list",list);
        return map;
    }

    public History getHistory(Balance balance,String info){
        History history = new History();
        history.setId(idWorker.nextId());
        history.setAccount(balance.getAccount());
        history.setInfo(info+balance.getBalance());
        return history;
    }
}
